#include "StudentInfo.h"
#include <iostream>
using namespace std;
StudentInfo::StudentInfo(){}
StudentInfo::StudentInfo(string name, string stuId, string grade, string pId, double score, double avgCredit, string username, string password)
{
    this->name = name;
    this->stuId = stuId;
    this->grade = grade;
    this->pId = pId;
    this->score = score;
    this->avgCredit = avgCredit;
    this->username = username;
    this->password = password;
}
// cout到显示器，调试阶段使用
void StudentInfo::showAll()
{
    cout << "姓名：" << this->name << " 学号：" << this->stuId << endl;
    cout << "年级：" << this->grade << " 身份证号：" << this->pId << endl;
    cout << "总学分：" << this->score << " 平均绩点：" << this->avgCredit<< endl;
    cout << "用户名：" << this->username << " 密码：" << this->password << endl;
}
// 学生个人查看时的字符串构造方法
string StudentInfo::toString()
{
    string ret = "";
    ret += "姓名： ";
    ret += name;
    ret += "\n";
    ret += "学号： ";
    ret += stuId;
    ret += "\n";
    ret += "年级： ";
    ret += grade;
    ret += "\n";
    ret += "身份证号： ";
    ret += pId;
    ret += "\n";
    ret += "已修学分： ";
    ret += to_string(score).substr(0, 5);
    ret += "\n";
    ret += "平均绩点： ";
    ret += to_string(avgCredit).substr(0, 3);
    ret += "\n";
    ret += "用户名： ";
    ret += username;
    ret += "\n";
    ret += "密码： ";
    ret += password;

    return ret;
}
// 老师查看多行时的字符串构造方法
string StudentInfo::sformat()
{
    string ret = "";
    ret += string(3, ' ');
    // 姓名最大长度为6
    ret += stringExtend(name, 6);
    ret += string(2, ' ');
    ret += stringExtend(stuId, 15);
    ret += string(2, ' ');
    ret += grade;
    ret += string(2, ' ');
    ret += stringExtend(pId, 18);
    ret += string(3, ' ');
    ret += to_string(score).substr(0, 5);
    ret += string(3, ' ');
    ret += to_string(avgCredit).substr(0, 3);
    ret += string(3, ' ');
    // 用户名最长为6
    ret += stringExtend(username, 6);
    ret += string(2, ' ');
    // 密码最长为10
    ret += stringExtend(password, 10);
    ret += "\n";
    return ret;
}
// 格式化字符串，生产长度为len的字符串，前面用空格补齐
string StudentInfo::stringExtend(string cur, int len)
{
    int curlen = cur.size();

    int dist = len - curlen;
//        cout << len << "-" << curlen << "=" << dist << endl;
    if (dist > 0)
    {
        string space(dist, ' ');
        return space + cur;
    }
    else if (dist < 0)
    {
        cout << cur << "Error: string too long" << endl;
        exit(0);
    }
    return cur;
}
// name的get/set方法
string StudentInfo::getName()
{
    return this->name;
}
void StudentInfo::setName(string name)
{
    this->name = name;
}
// stuId的get/set方法
string StudentInfo::getStuId()
{
    return this->stuId;
}
void StudentInfo::setStuId(string stuId)
{
    this->stuId = stuId;
}
// grade的get/set方法
string StudentInfo::getGrade()
{
    return this->grade;
}
void StudentInfo::setGrade(string grade)
{
    this->grade = grade;
}
// pId的get/set方法
string StudentInfo::getPId()
{
    return this->pId;
}
void StudentInfo::setPId(string pId)
{
    this->pId = pId;
}
// score的get/set方法
double StudentInfo::getScore()
{
    return this->score;
}
void StudentInfo::setScore(double score)
{
    this->score = score;
}
// avgCredit的get/set方法
double StudentInfo::getCredit()
{
    return this->avgCredit;
}
void StudentInfo::setCredit(double credit)
{
    this->avgCredit = credit;
}
// username的get/set方法
string StudentInfo::getUsername()
{
    return this->username;
}
void StudentInfo::setUsername(string username)
{
    this->username = username;
}
// password的get/set方法
string StudentInfo::getPassword()
{
    return this->password;
}
void StudentInfo::setPassword(string password)
{
    this->password = password;
}
